package com.partner.frame.support.util;

import java.text.*;
import net.sf.json.*;
import net.sf.json.processors.*;

public class JsonDateValueProcessor implements JsonValueProcessor
{
   //private String dateFormat = "yyyy-MM-dd HH:mm:ss";
   
   public JsonDateValueProcessor()
   {
   }

   public Object processArrayValue(Object objValue, JsonConfig jsonConfig)
   {
      int i = 0;
      String[] objArray = {};
      SimpleDateFormat fmtDate = null;
      java.util.Date[] dateArray = null;

      if(objValue == null) return objArray;

      if(objValue instanceof java.util.Date[])
      {
         fmtDate = new SimpleDateFormat("yyyy-MM-dd");
         dateArray = (java.util.Date[]) objValue;

         objArray = new String[dateArray.length];
         for(i = 0; i < dateArray.length; i++)
         {
            objArray[i] = fmtDate.format(dateArray[i]);
         }
      }
      else if(objValue instanceof java.sql.Date[])
      {
         fmtDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         dateArray = (java.sql.Date[]) objValue;

         objArray = new String[dateArray.length];
         for(i = 0; i < dateArray.length; i++)
         {
            objArray[i] = fmtDate.format(dateArray[i]);
         }
      }

      return objArray;
   }

   public Object processObjectValue(String objKey, Object objValue, JsonConfig jsonConfig)
   {
      String strResult = "";

      if(objValue == null) return "";

      // 注意：在判断几个父子级类型时要先判断子类型再判断父类型
      // java.sql.Timestamp,java.sql.Date继承java.util.Date
      if(objValue instanceof java.sql.Timestamp)
      {
         strResult = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((java.sql.Timestamp) objValue);
         //System.out.println("java.sql.Timestamp - " + strResult);
         return strResult;
      }
      else if(objValue instanceof java.sql.Date)
      {
         strResult = new SimpleDateFormat("yyyy-MM-dd").format((java.sql.Date) objValue);
         //System.out.println("java.sql.Date - " + strResult);
         return strResult;
      }
      else if(objValue instanceof java.util.Date)
      {
         strResult = new SimpleDateFormat("yyyy-MM-dd").format((java.util.Date) objValue);
         //System.out.println("java.util.Date - " + strResult);
         return strResult;
      }

      return objValue.toString();
   }

}
